<?php
include_once('includes/classes/team.php');
include_once('includes/classes/league.php');
include_once("actions.php");
class Teams extends Actions {
	function action($post_vars) {
		global $session;
		
		$return_str = "";
		if (!$session->isAdmin()) 
			$return_str .= parent::soon($post_vars, "yetersiz yetki");
		else {
			switch ($post_vars['action']) {
				default:
				case "Form":
					$return_str .= $this->Form($post_vars);
					$return_str .= $this->View ($post_vars);
					break;
				case "Delete":
					$return_str .= $this->Delete ($post_vars);
					$return_str .= $this->View ($post_vars);				
					break;
				case "Update":
					$return_str .= $this->Update ($post_vars);
					$return_str .= $this->View ($post_vars);				
					break;
				case "Add":
					$return_str .= $this->Add ($post_vars);
					$return_str .= $this->View ($post_vars);
					break;
			}
		}
		return $return_str;
	}
	function Validate ($post_vars) {
		extract ($post_vars);	
		if ($Team == "")
			return "Takim ismi eksik.";		
	}	
	//-----------------------------------------
	function Form ($post_vars=NULL, $error=NULL) {
		if (isset($post_vars))
			extract ($post_vars);
		if (isset($leagueId))
			return "";
		require_once("ui/init.php");
		$smarty = new _Smarty();
		
		if (isset($Id)) {
			$team = new Team(0,"0","0");
			if ($team->set($Id) < 1) 
				$error = "Takim Sorgusu hatali ($Id)";
			$smarty->assign ("team", $team);
		}
		$smarty->assign ($post_vars);
		if ($error != NULL)
			$smarty->assign ("error", $error);
		return $smarty->fetch ('teamForm.html');
	}	
	//-----------------------------------------
	function View ($post_vars) {
		if (isset($post_vars)) {
			$globals = new Globals();
			$post_vars = $globals->AddSlashArray ($post_vars);
			extract ($post_vars);
		}			
		$team = new Team(0,"0","0");
		require_once("ui/init.php");
		$smarty = new _Smarty ();
		foreach ($team->getList() as $row) {
			$t = new Team($row["Id"], $row["Team"], $row["Memo"]);
			if (isset ($leagueId)) 
				$t->setLeague($leagueId);
			$smarty->append('teams', $t);
			unset($t);
		}
		if (isset ($leagueId)) {
			$l = new League(0, "", "");
			$l->set($leagueId);
			$smarty->assign("league", $l);
		}		
		$smarty->assign($post_vars);	
		return $smarty->fetch ('teamList.html');
	
	}	
	//-----------------------------------------
	function Add ($post_vars) {
		if ($this->addLeague($post_vars) > 0)
			return "";
		require_once("ui/init.php");
		$smarty = new _Smarty ();
		
		$error = $this->Validate ($post_vars);
		
		if ($error != "") {
			return $this->Form ($post_vars, $error);
		} else {
			$globals = new Globals();
			$post_vars = $globals->AddSlashArray ($post_vars);
			extract ($post_vars);
			$league = new League(0, $league, $season);
			if (!$league->Add()) 
				$smarty->assign ("error", "Couldn't add $first $last to database.");	
			return $this->Form($post_vars);
		}
	}	
	//-----------------------------------------
	function addLeague($post_vars) {
		$globals = new Globals();
		$post_vars = $globals->AddSlashArray ($post_vars);
		extract ($post_vars);		
		if (isset($leagueId) && isset($Id)) {
			$t = new Team($Id, "", "");
			$t->assignIntoLeague($leagueId);
			return 1;
		}
		return 0;
	}
	//-----------------------------------------
	function Delete ($post_vars) {
		if ($this->deleteLeague($post_vars) > 0)
			return "";		
		require_once("ui/init.php");
		$smarty = new _Smarty ();
			
		$globals = new Globals();
		$post_vars = $globals->AddSlashArray ($post_vars);
		extract ($post_vars);
		$league = new League($id, "", "");
		if (!$league->Delete()) 
			$smarty->assign ("error", "Couldn't add $first $last to database.");	
		return $this->Form($post_vars);
	
	}		
	//-----------------------------------------
	function deleteLeague($post_vars) {
		$globals = new Globals();
		$post_vars = $globals->AddSlashArray ($post_vars);
		extract ($post_vars);		
		if (isset($leagueId) && isset($Id)) {
			$t = new Team($Id, "", "");
			$t->deleteFromLeague($leagueId);
			return 1;
		}
		return 0;
	}	
	//-----------------------------------------
	function Update ($post_vars) {
		require_once("ui/init.php");
		$smarty = new _Smarty ();
		
		$error = $this->Validate ($post_vars);
		
		if ($error != "") {
			return $this->Form ($post_vars, $error);
		} else {
			$globals = new Globals();
			$post_vars = $globals->AddSlashArray ($post_vars);
			extract ($post_vars);
			$team = new Team($Id, $Team, $Memo);
			if (!$team->Update()) 
				$smarty->assign ("error", "Couldn't add $first $last to database.");	
			return $this->Form();
		}
	}		
}
?>